package com.lcq.study.leetcode;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

/**
 * @ClassName T20220419
 * @Description
 * @Author lichuanqi
 * @Date 2022/4/19 14:23
 * @Version 1.0
 **/
public class T20220419 {

    public static void main(String[] args) {
        T20220419 test = new T20220419();
        int[] nums = {3,3};
        boolean b = test.containsDuplicate1(nums);
        System.out.println(b);

        int[] nums1 = {4,1,2,1,2};
        System.out.println(test.singleNumber(nums1));
    }

    public boolean containsDuplicate(int[] nums) {
        if (nums.length < 2){
            return false;
        }

        HashMap<Integer, Boolean> map = new HashMap<>(nums.length);
        for (int i = 0; i < nums.length; i++) {
            Boolean aBoolean = map.get(nums[i]);
            if (aBoolean != null){
                return true;
            }
            map.put(nums[i], true);
        }
        return false;
    }

    public boolean containsDuplicate1(int[] nums) {
        if (nums.length < 2){
            return false;
        }

        Set<Integer> set = new HashSet<>(nums.length);
        for (int i = 0; i < nums.length; i++) {
            Boolean aBoolean = set.add(nums[i]);
            if (aBoolean == false){
                return true;
            }
        }
        return false;
    }

    public int singleNumber(int[] nums) {
        for (int i = 0; i < nums.length - 1; i++) {
            nums[i+1] ^= nums[i];
        }
        return nums[nums.length-1];
    }
}
